package cn.green.core.adapt.comps.field.type;

import cn.green.core.common.enums.field.ColumnType;
import cn.green.core.components.database.session.dml.DeleteSession;
import cn.green.core.components.database.session.dml.InsertSession;
import cn.green.core.components.database.session.dml.UpdateSession;
import cn.green.core.components.entity.ColumnLength;
import cn.green.core.components.entity.JoinInfo;
import cn.green.core.components.entity.QCondition;
import cn.green.core.components.field.FieldTypeCom;
import cn.green.core.excel.model.Column;
import cn.green.core.front.model.Fetch;
import cn.green.core.front.model.Input;
import cn.green.core.front.model.Title;
import cn.green.core.model.QFieldModel;
import cn.green.core.model.QTableModel;
import cn.green.core.quick.ThreadDataStore;
import cn.green.core.quick.cache.comp.Key;
import cn.green.core.scanner.anno.comps.QTableComponent;
import cn.green.manage.service.QDqlService;
import cn.green.utils.OrmAppUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wang.yishu
 * @version 1.0
 * @className OneToManyType
 * @description 一对多字段，为虚拟类型
 * //todo 等待实现
 * @date 2025/5/14 14:50
 */
@QTableComponent
public class OneToManyType implements FieldTypeCom {
    @Override
    public String fieldTypeName() {
        return "ORM_ONE_TO_MANY";
    }

    @Override
    public String fieldTypeCode() {
        return "ORM_ONE_TO_MANY";
    }

    @Override
    public ColumnType getColumnType(QFieldModel fieldModel) {
        return ColumnType.NONE;
    }

    @Override
    public ColumnLength getColumnLength(QFieldModel fieldModel) {
        return null;
    }

    @Override
    public boolean canBeSelected() {
        return false;
    }

    @Override
    public void formatRecord(JSONObject record, QFieldModel field) {
        JSONObject configObj = (JSONObject) field.getConfigObj();
        String targetSource = configObj.getString("targetSource");
        String targetScheme = configObj.getString("targetScheme");
        String targetTableName = configObj.getString("targetTableName");
        JSONArray targetData = ThreadDataStore.get(new Key("ORM_ONE_TO_MANY:TABLE:" + targetSource + ":" + targetScheme + ":" + targetTableName), JSONArray.class, new JSONArray(),
                () -> {
                    QDqlService dqlService = OrmAppUtils.getBean(QDqlService.class);
                    return dqlService.list(targetSource, targetScheme, targetTableName, new JSONObject());
                });
        JSONObject columnMap = configObj.getJSONObject("columnMap");
        if (targetData != null && !targetData.isEmpty()) {
            List<Object> tarData = targetData.stream().filter(item -> {
                boolean hit = true;
                for (String cur : columnMap.keySet()) {
                    String tar = columnMap.getString(cur);
                    if (!Objects.equals(((JSONObject) item).get(tar), record.get(cur))) {
                        hit = false;
                        break;
                    }
                }
                return hit;
            }).collect(Collectors.toList());
            record.put(field.getFieldName(), tarData);
        }
    }

    @Override
    public void parseRecord(JSONObject record, QFieldModel field) {
    }

    @Override
    public List<Input> inputs(QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Title> titles(QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Fetch> fetches(QTableModel table, QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<QCondition> conditions(QFieldModel field, JSONObject param) {
        return Collections.emptyList();
    }

    @Override
    public List<JoinInfo> joinInfos(QTableModel table, QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Column> columns(QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public boolean isVirtual() {
        return true;
    }

    @Override
    public InsertSession insertSession(QTableModel table, QFieldModel field, JSONObject record) {
        return FieldTypeCom.super.insertSession(table, field, record);
    }

    @Override
    public UpdateSession updateSession(QTableModel table, QFieldModel field, JSONObject record, List<String> conditions) {
        return FieldTypeCom.super.updateSession(table, field, record, conditions);
    }

    @Override
    public DeleteSession deleteSession(QTableModel table, QFieldModel field, JSONObject record, List<String> conditions) {
        return FieldTypeCom.super.deleteSession(table, field, record, conditions);
    }
}
